package com.zhq.controller;

import com.zhq.bean.*;
import com.zhq.mvc.ResponseBody;
import com.zhq.service.CourierService;
import com.zhq.service.UserService;
import com.zhq.util.DateFormatUtil;
import com.zhq.util.JSONUtil;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.DateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @Author:钟豪强
 * @Description:    courier的有关操作
 * @Date Created in  2020-08-18 18:18
 * @Modified By:
 */
public class CourierController {

    @ResponseBody("/courier/console.do")
    public String console(HttpServletRequest request, HttpServletResponse response){
        Map<String, Integer> data = CourierService.console();
        Message msg = new Message();
        if (data.size() == 0) {
            msg.setStatus(-1);
        }else{
            msg.setStatus(0);
        }
        msg.setData(data);
        String json = JSONUtil.toJSON(msg);
        return json;
    }

    @ResponseBody("/courier/list.do")
    public String list(HttpServletRequest request, HttpServletResponse response){
        //1.接收参数
        int offset = Integer.parseInt(request.getParameter("offset"));
        int pageNumber = Integer.parseInt(request.getParameter("pageNumber"));
        System.out.println(offset);
        System.out.println(pageNumber);
        List<Courier> list = CourierService.findAll(true, offset, pageNumber);
        System.out.println(list);
        ArrayList<BootStrapTableCourier> list1 = new ArrayList<>();
        //2.将Courier转化为BootStrapTableCourier
        for (Courier c:list) {
            String createTime = DateFormatUtil.format(c.getCreateTime());
            String lastLoginTime = c.getLastLoginTime() == null ? "":DateFormatUtil.format(c.getLastLoginTime());
            BootStrapTableCourier b = new BootStrapTableCourier(c.getId(),c.getUsername(),c.getUserPhone(),c.getUserNumber(),c.getPassword(),c.getExpressNumber(),createTime,lastLoginTime);
            list1.add(b);
        }
        //3.将BootStrapTableCourier集合返回出去
        Map<String, Integer> console = CourierService.console();
        Integer total = console.get("data_size");
        ResultData<BootStrapTableCourier> resultData = new ResultData<>();
        resultData.setRows(list1);
        resultData.setTotal(total);
        String json = JSONUtil.toJSON(resultData);
        return json;
    }

    @ResponseBody("/courier/insert.do")
    public String insert(HttpServletRequest request, HttpServletResponse response){
        //1.    接收参数
        String username = request.getParameter("username");
        String userPhone = request.getParameter("userPhone");
        String userNumber = request.getParameter("userNumber");
        String password = request.getParameter("password");
        boolean flag = CourierService.insert(username, userPhone, userNumber, password);
        Message msg = new Message();
        if (flag) {
            msg.setStatus(0);
            msg.setResult("添加成功");
        }else{
            msg.setStatus(-1);
            msg.setResult("添加失败");
        }
        String json = JSONUtil.toJSON(msg);
        return json;
    }

    @ResponseBody("/courier/find.do")
    public String find(HttpServletRequest request, HttpServletResponse response){
        //1.接收参数
        String userPhone = request.getParameter("g_userPhone");
        Courier courier = CourierService.findByUserPhone(userPhone);
        Message msg = new Message();
        if (courier != null) {
            msg.setStatus(0);
        }else{
            msg.setStatus(-1);
        }
        msg.setData(courier);
        String json = JSONUtil.toJSON(msg);
        return json;
    }

    @ResponseBody("/courier/update.do")
    public String update(HttpServletRequest request, HttpServletResponse response){
        String username = request.getParameter("username");
        String userPhone = request.getParameter("userPhone");
        String userNumber = request.getParameter("userNumber");
        String password = request.getParameter("password");
        String g_userPhone = request.getParameter("g_userPhone");
        int id = Integer.parseInt(request.getParameter("userId"));

        Courier courier = new Courier();
        courier.setUsername(username);
        courier.setUserPhone(userPhone);
        courier.setUserNumber(userNumber);
        courier.setPassword(password);
        courier.setG_userPhone(g_userPhone);
        boolean flag = CourierService.update(id, courier);
        Message msg = new Message();
        if (flag) {
            msg.setStatus(0);
            msg.setResult("修改成功");
        }else {
            msg.setStatus(-1);
            msg.setResult("修改成功");
        }
        String json = JSONUtil.toJSON(msg);
        return json;
    }

    @ResponseBody("/courier/delete.do")
    public String delete(HttpServletRequest request, HttpServletResponse response){
        //1.接收参数
        int id = Integer.parseInt(request.getParameter("userId"));
        boolean flag = CourierService.delete(id);
        Message msg = new Message();
        if (flag) {
            msg.setStatus(0);
            msg.setResult("删除成功");
        }else{
            msg.setStatus(-1);
            msg.setResult("删除失败");
        }
        String json = JSONUtil.toJSON(msg);
        return json;
    }

    @ResponseBody("/courier/checkName.do")
    public String checkName(HttpServletRequest request, HttpServletResponse response){
        String username = request.getParameter("username");
        List<Courier> list = CourierService.findAll(false, 0, 0);
        Message msg = new Message();
        msg.setStatus(0);
        for (Courier c:list) {
            if (username.equals(c.getUsername())) {
                msg.setStatus(-1);
                msg.setResult("该用户名已被使用");
            }
        }
        return JSONUtil.toJSON(msg);
    }

    @ResponseBody("/courier/checkPhone.do")
    public String checkPhone(HttpServletRequest request, HttpServletResponse response){
        String userPhone = request.getParameter("userPhone");
        List<Courier> list = CourierService.findAll(false, 0, 0);
        Message msg = new Message();
        msg.setStatus(0);
        for (Courier c:list) {
            if (userPhone.equals(c.getUserPhone())) {
                msg.setStatus(-1);
                msg.setResult("该手机号码已存在");
            }
        }
        return JSONUtil.toJSON(msg);
    }

    @ResponseBody("/courier/checkNumber.do")
    public String checkNumber(HttpServletRequest request, HttpServletResponse response){
        String userNumber = request.getParameter("userNumber");
        List<Courier> list = CourierService.findAll(false, 0, 0);
        Message msg = new Message();
        msg.setStatus(0);
        for (Courier c:list) {
            if (userNumber.equals(c.getUserNumber())) {
                msg.setStatus(-1);
                msg.setResult("该身份证已被使用");
            }
        }
        return JSONUtil.toJSON(msg);
    }
}
